package com.ruoyi.app.service.impl

import com.aliyun.imageseg20191230.models.SegmentCommonImageRequest
import com.aliyun.tea.TeaException
import com.aliyun.teautil.Common
import com.aliyun.teautil.models.RuntimeOptions
import com.ruoyi.app.common.*
import com.ruoyi.app.service.AliApiService
import com.ruoyi.app.service.SegmentCommonImageDto
import com.ruoyi.exception.FlyException
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Service
import java.util.*

@Service
class AliApiServiceImpl : AliApiService {

    @Autowired
    private lateinit var netUrlUploadOss: NetUrlUploadOss


    override fun addSegmentCommonImage(segmentCommonImageDto: SegmentCommonImageDto): Data<String> {
        val client = Sample.createClient(
            Sign.accessKeyId,
            Sign.accessSecret
        )
        val segmentCommonImageRequest = SegmentCommonImageRequest()
        val runtime = RuntimeOptions()
        try {
            segmentCommonImageRequest.setImageURL(segmentCommonImageDto.imageURL?.trans())
                .setReturnForm(segmentCommonImageDto.returnForm)
            // 复制代码运行请自行打印 API 的返回值
            val segmentCommonImageResponse = client.segmentCommonImageWithOptions(segmentCommonImageRequest, runtime)
            return Ok("获取成功", netUrlUploadOss.upload(segmentCommonImageResponse.body.data.imageURL))

        } catch (error: TeaException) {
            // 如有需要，请打印 error
            throw FlyException(error.message)
        } catch (_error: java.lang.Exception) {
            val error = TeaException(_error.message, _error)
            throw FlyException(error.message)
        }
    }
}